package com.jjs.jcenterdemo;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorEventListener2;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.white.commonlib.manager.LogManager;

public class SensorActivity extends AppCompatActivity implements SensorEventListener
{

    private SensorManager sm;
    private Sensor gyroscopeSensor;
    private Sensor magneticSensor;
    private Sensor accelerometerSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);//不显示程序的标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//不显示系统的标题栏
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sensor);

        initSensor();
    }

    private void initSensor()
    {
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        gyroscopeSensor = sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        magneticSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        accelerometerSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    }

    private boolean isset;

    public void click(View view)
    {
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if (gyroscopeSensor != null)
            sm.registerListener(this, gyroscopeSensor, SensorManager.SENSOR_DELAY_NORMAL);

        sm.registerListener(this, magneticSensor, SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        sm.unregisterListener(this);
    }

    private float acMaxValue;

    // 将纳秒转化为秒

    private static final float NS2S = 1.0f / 1000000000.0f;
    private float timestamp;
    private float angle[] = new float[3];

    @Override
    public void onSensorChanged(SensorEvent event)
    {

        switch (event.sensor.getType())
        {
            case Sensor.TYPE_ACCELEROMETER:
                // x,y,z分别存储坐标轴x,y,z上的加速度
                float x = event.values[0];
                float y = event.values[1];
                float z = event.values[2];
                // 根据三个方向上的加速度值得到总的加速度值a
                float a = (float) Math.sqrt(x * x + y * y + z * z);

                if (acMaxValue < a)
                    acMaxValue = a;

                LogManager.e("加速传感器平均加速度---------->" + a + " 最大加速度------->" + acMaxValue);
                LogManager.e("加速传感器最低延迟-------->" + magneticSensor.getMinDelay());
                // 加速度传感器的最大量程
                LogManager.e("加速传感器最大量程-------->" + event.sensor.getMaximumRange());
                break;
            case Sensor.TYPE_MAGNETIC_FIELD:
                break;
            case Sensor.TYPE_GYROSCOPE:
                if (timestamp != 0)
                {
                    // 得到两次检测到手机旋转的时间差（纳秒），并将其转化为秒
                    final float dT = (event.timestamp - timestamp) * NS2S;
                    // 将手机在各个轴上的旋转角度相加，即可得到当前位置相对于初始位置的旋转弧度
                    angle[0] += event.values[0] * dT;
                    angle[1] += event.values[1] * dT;
                    angle[2] += event.values[2] * dT;

                    // 将弧度转化为角度
                    float anglex = (float) Math.toDegrees(angle[0]);
                    float angley = (float) Math.toDegrees(angle[1]);
                    float anglez = (float) Math.toDegrees(angle[2]);
                    System.out.println("anglex------------>" + anglex);
                    System.out.println("angley------------>" + angley);
                    System.out.println("anglez------------>" + anglez);
                    break;
                }
                //将当前时间赋值给timestamp
                timestamp = event.timestamp;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i)
    {

    }
}
